package com.hb.medical_research_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.medical_research_system.common.Results;
import com.hb.medical_research_system.entity.Search;
import com.hb.medical_research_system.mapper.SearchMapper;
import com.hb.medical_research_system.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hb
 * @description 针对表【search】的数据库操作Service实现
 * @createDate 2023-02-25 11:15:05
 */
@Service
public class SearchServiceImpl extends ServiceImpl<SearchMapper, Search> implements SearchService {

    @Autowired
    private SearchService searchService;

    @Autowired
    private SearchMapper searchMapper;

    /**
     * 添加搜索记录
     *
     * @param search
     * @return
     */
    @Override
    public Results add(Search search) {
        search.setTime(LocalDateTime.now());
        searchMapper.insert(search);
        return Results.data(Results.SUCCESS, search, "保存新搜索记录成功");
    }

    /**
     * 根据用户id查询搜索记录
     *
     * @param userId
     * @return
     */
    @Override
    public Page<Search> findByUserId(Long userId, int page, int pageSize) {
        Page pageInfo = new Page(page, pageSize);

        LambdaQueryWrapper<Search> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(userId != null, Search::getUserId, userId);

        searchService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    @Override
    public Results findSearchByUserId(Long userId) {
        QueryWrapper<Search> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<Search> searches = searchMapper.selectList(queryWrapper);
        if (searches == null)
            return Results.info(Results.ERROR, "用户无记录");
        else
            return Results.data(Results.SUCCESS, searches, "查询用户记录成功");
    }

}




